package com.zjm.cookie.servlet;

import com.zjm.cookie.utils.CookieUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;

/**
 * @author ZJM
 * @date 2024/4/23 22:05
 */
@WebServlet("/cookieServlet")
public class CookieServlet extends BaseServlet {

    /**
     * 创建Cookie
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void createCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.创建Cookie对象
        Cookie cookie = new Cookie("key1", "zjm");
        //2.通知客户端保存Cookie
        resp.addCookie(cookie);
        Cookie cookie1 = new Cookie("key2", "zjm2");
        resp.addCookie(cookie1);

        // Cookie编码处理中文等特殊字符
        Cookie cookie2 = new Cookie(URLEncoder.encode("姓名", "UTF-8"), URLEncoder.encode("猫玖", "UTF-8"));
        resp.addCookie(cookie2);

        resp.getWriter().write("Cookie创建成功");
    }

    /**
     * 获取浏览器Cookie
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void getCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie[] cookies = req.getCookies();
        for (Cookie cookie : cookies) {
            //getName()获取cookie的key
            //getValue()获取cookie的value
            // resp.getWriter().write(cookie.getName() + "=" + cookie.getValue() + "<br/>");
            resp.getWriter().write(URLDecoder.decode(cookie.getName(), "UTF-8") + "=" + URLDecoder.decode(cookie.getValue(), "UTF-8") + "<br/>");
        }

        //获取某个具体的Cookie
        // Cookie cookie1 = null;
        // for (Cookie cookie : cookies) {
        //     if ("key1".equals(cookie.getName())) {
        //         cookie1 = cookie;
        //         break;
        //     }
        // }
        //使用自己的工具类
        Cookie cookie1 = CookieUtils.findCookie("key1", cookies);
        if (cookie1 != null) {
            resp.getWriter().write("找到要的cookie:" + cookie1.getName() + "=" + cookie1.getValue());
        }

        //Cookie解码处理中文等特殊字符
        Cookie cookie2 = CookieUtils.findCookie(URLEncoder.encode("姓名", "UTF-8"), cookies);
        if (cookie2 != null) {
            resp.getWriter().write("找到要的cookie:" + URLDecoder.decode(cookie2.getName(), "UTF-8") + "=" + URLDecoder.decode(cookie2.getValue(), "UTF-8"));
        }
    }

    /**
     * Cookie值的修改
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void updateCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 方案一：
        // 1.创建一个要修改的同名的Cookie对象
        // 2.在构造器，同时赋予新的Cookie值
        Cookie cookie = new Cookie("key1", "newValue");
        // 3.调用response.addCookie(Cookie)
        resp.addCookie(cookie);
        resp.getWriter().write("修改成功");

        // 方案二
        // 1. 先查找需要修改的Cookie对象
        Cookie cookie2 = CookieUtils.findCookie("key2", req.getCookies());
        if (cookie2 != null) {
            // 2. 调用setValue()方法赋予新的Cookie值
            cookie2.setValue("newValue2");
            // 3. 通知客户端保存修改
            resp.addCookie(cookie2);
        }
        resp.getWriter().write("修改成功2");
    }

    public void defaultLife(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("defaultLife", "defaultLife");
        cookie.setMaxAge(-1); //设置存活时间
        resp.addCookie(cookie);

    }

    public void deleteNow(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = CookieUtils.findCookie("key1", req.getCookies());
        if (cookie != null) {
            cookie.setMaxAge(0); //立即删除
            resp.addCookie(cookie);
            resp.getWriter().write("key1删除");
        }
    }

    public void life3600(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("life3600", "test");
        cookie.setMaxAge(60 * 60); //设置一小时后删除
        resp.addCookie(cookie);
        resp.getWriter().write("设置一小时后删除");
    }

    public void testPath(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("testPath", "testPath");
        cookie.setPath(req.getContextPath() + "/abc");
        resp.addCookie(cookie);
        resp.getWriter().write("创建成功");
    }
}
